﻿// See https://aka.ms/new-console-template for more information
using System.Net.Http.Headers;

Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine("Hello, World!");

using (var client =new HttpClient())
{
    var token = "";

    var parameters = new Dictionary<string, string>
        {
            { "userName", "admin" },
            { "password", "password" }
        };

    var content = new FormUrlEncodedContent(parameters);
    Console.WriteLine("请求授权接口获取Token......");
    HttpResponseMessage response = await client.PostAsync("http://localhost:5000/Auth/GetToken", content);
    if (response.IsSuccessStatusCode)
    {
        token =await response.Content.ReadAsStringAsync();
        Console.WriteLine("获取Token成功：");
        Console.WriteLine(token);
    }
    else
    {
        Console.WriteLine("获取Token失败！");
        return;
    }

    Console.WriteLine("未设置Token时请求Authorize接口...");


    response = await client.GetAsync("http://localhost:5000/Auth/TestAuth");
    Console.WriteLine($"请求Authorize接口状态码：{response.StatusCode}");

    Console.WriteLine("设置Token...");
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

    response = await client.GetAsync("http://localhost:5000/Auth/TestAuth");
    Console.WriteLine($"请求Auth 接口状态码：{response.StatusCode}");
    Console.WriteLine($"请求Auth 接口结果：{await response.Content.ReadAsStringAsync()}");

    response = await client.GetAsync("http://localhost:5000/Auth/TestAuthAdmin");
    Console.WriteLine($"请求Admin Auth接口状态码：{response.StatusCode}");
    Console.WriteLine($"请求Admin Auth接口结果：{await response.Content.ReadAsStringAsync()}");
}

Console.ReadKey();